<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/resources/modular/plugin/taglibs.jsp" %>
<!doctype html>
<head content="-系统用户管理">
    <%@ include file="/resources/modular/head/inside_factory_head.jsp" %>

    <style type="text/css">

        .pointpage_dv1, .pointpage_dv2,
        .pointpage_dv3, .pointpage_dv4,
        .my_childAccount_managedv, .my_childAccount_pointpage,
        .my_childAccount_addchildrendv {
            display: none;
        }

        .pointpage_psddv, .pointpage_psdpoint, .pointpage_dv {
            display: none;
        }

        .users_system_managedv, .users_system_pointpage,
        .users_system_addchildrendv {
            display: none;
            position: relative;
            top: 0px;
            left: -630px;
        }

        .my_childAccount_pointpagedv_ul ul {
            display: none;
        }

        .my_childAccount_pointpagedv_ul > li > p, .my_childAccount_pointpagedv_rightul > li > p {
            text-indent: 20px;
            line-height: 40px;
            color: #666;
            border-bottom: 1px solid #eee;
        }

        .my_childAccount_addBtn, .my_childAccount_removeBtn {
            display: none;
            font-style: normal;
        }

        .my_childAccount_pointpagedv_left .my_childAccount_removeBtn {
            display: inline;
            color: #f00;
            font-size: 14px;
        }

        .my_childAccount_pointpagedv_right .my_childAccount_addBtn {
            display: inline;
            color: #38adff;
            font-size: 14px;
        }
    </style>
</head>
<body class="GC_Menu_3" name="GC_systemuser">
<%@ include file="/resources/modular/plugin/mainFactoryMenu.jsp" %>
<div class="loading"></div>
<div class="page" name="GC_systemuser">

    <%@ include file="/resources/modular/plugin/mainFactoryTopMenu.jsp" %>
    <%@ include file="/resources/modular/plugin/usersmanageMeun.jsp" %>
    <div class="page_line"></div>

    <div class="page_right userspage_right">
        <div class="users_system_pointdv">
            <p>* 系统初始密码默认为：123456</p>
            <p>* 修改密码，若不需要设置新密码，可直接恢复初始密码</p>
        </div>
        <ul class="users_system_rightTitle">
            <li>名称</li>
            <li>创建时间</li>
            <li>操作</li>
        </ul>

        <c:forEach items="${systemroles}" var="sysrole" varStatus="status">
            <div class="users_system_rightList">
                <div>${sysrole.name}</div>
                <div roleid="${sysrole.id}">
                    <a class="users_system_manage" href="javascript:;" id="users_system_manage_${sysrole.id}">管理</a>
                    <a class="users_system_impower" href="javascript:showrolemodules(${sysrole.id});">授权</a>
                    <a class="users_system_add" href="javascript:;">添加账户</a>
                        <%-- 用户管理 begin --%>
                    <div class="users_system_managedv" id="GC_SYSTEMUSER_${sysrole.id}">

                    </div>
                        <%-- 用户管理 end --%>

                    <!-- 授权管理 -->
                    <div class="users_system_pointpage" style="background:#EEE;">
                        <div class="my_childAccount_pointpagedv">
                            <div class="my_childAccount_pointpagedv_left">
                                <p class="my_childAccount_pointpagep2">已选授权名称</p>
                                <ul class="my_childAccount_pointpagedv_ul" id="GC_MODULE_USEDMODULE_${sysrole.id}">
                                        <%--动态追加内容--%>
                                </ul>
                            </div>

                            <div class="my_childAccount_pointpagedv_right">
                                <p class="my_childAccount_pointpagep2">可选授权名称</p>
                                <ul class="my_childAccount_pointpagedv_ul" id="GC_MODULE_AVAILABLEMODULE_${sysrole.id}">
                                        <%--动态追加内容--%>
                                </ul>
                            </div>
                        </div>
                        <p class="users_system_pointpagep3">
                            <a class="users_system_addchildbtn1" href="javascript:;" GC_ROLEID="${sysrole.id}">确认</a>
                            <a class="users_system_addchildbtn2" href="javascript:;">取消</a></p>
                    </div>
                    <!-- 授权管理 end -->


                        <%-- 添加账户 begin --%>
                    <div class="users_system_addchildrendv">
                        <p>子账户名称：<input id="users_system_name_${sysrole.id}" class="users_system_addchildrenname"
                                        type="text" placeholder="请输入子账户名称"></p>
                        <p>子账户密码：<input id="users_system_pass_${sysrole.id}" class="users_system_addchildrenpsd"
                                        type="password" placeholder="请输入子账户密码"></p>
                        <span style="display:inline-block;margin-left:432px;">
                            <a class="users_system_addchildrendvbtn1" href="javascript:;" roleid="${sysrole.id}">确认</a>
                            <a class="users_system_addchildrendvbtn2" href="javascript:;">取消</a>
                        </span>
                    </div>
                        <%-- 添加账户 end --%>
                </div>
            </div>
        </c:forEach>


    </div>


</div>

<!-- 修改密码 提示弹窗 -->
<div class="pointpage_psddv">
    <div class="pointpage_psd">
        <p class="pointpage_psdtitle">
            <strong class="pointpage_psdtitleName">修改密码（<font id="GC_SYSTEMUSER_USERNAME"></font>）</strong>
            <a href="javascript:;" class="pointpage_psdclose"></a>
        </p>
        <p class="pointpage_psdval">
            <span>新密码：</span>
            <input class="pointpage_psdvalue" type="password" id="GC_SYSTEMUSER_PASSWORD" placeholder="请输入新密码">
            <input type="hidden" id="GC_SYSTEMUSER_USERID" value="">
            <a class="pointpage_psdcfm" href="javascript:;">确认</a>
        </p>
        <p class="pointpage_psdpoint">您输入的密码有误</p>
    </div>
</div>
<!-- 修改密码 提示弹窗 end -->

<!-- 删除角色 提示弹窗 -->
<div class="pointpage_dv">
    <div class="pointpage">
        <p>是否删除子账号“<span id="GC_SYSTEMUSER_REMOVE_USERNAME">李四</span>”？</p>
        <p><a class="users_system_deletebtn1" href="javascript:;">确认</a> <a class="users_system_deletebtn2"
                                                                            href="javascript:;">取消</a></p>
        <input type="hidden" id="GC_SYSTEMUSER_REMOVE_USERID" value="">
    </div>
</div>
<!-- 删除角色 提示弹窗 end -->


</body>
</html>
<%@ include file="/resources/modular/foot/inside_factory_footer.jsp" %>
<script type="text/javascript" src="${basePath}resources/js/v2/youyicloud-md5.js"></script>
<script type="text/javascript" src="${basePath}resources/js/v2/factory/improve.js"></script>
<script type="application/javascript">

    var regName = /^\S{6,16}$/;
    var regPass = /^\S{6,16}$/;

    $(".users_system_addchildrendvbtn1").click(function () {
        var roleid = $(this).attr("roleid");
        var username = $("#users_system_name_" + roleid).val();
        var password = $("#users_system_pass_" + roleid).val();
        var usertype = 3;
        if (!regName.test(username)) {
            alert("用户名长度至少6位，最大16位");
        } else if (!regPass.test(password)) {
            alert("密码长度至少6位，最大16位");
        } else {
            $.ajax({
                url: '${basePath}v2/factory/individual/addsystemuser',
                type: 'POST',
                data: {
                    "username": username,
                    "password": createPassword(password, password),
                    "role": roleid,
                    "state": 1,
                    "ntype":usertype
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $(".users_system_addchildrendvbtn1[roleid="+roleid+"]").parents(".users_system_addchildrendv").stop().hide();
                        $(".users_system_addchildrendvbtn1[roleid="+roleid+"]").parents(".users_system_rightList").find(".users_system_add").css("background", "#FFF");
                        alert("子账户添加成功", "cur");
                    } else {
                        alert(ret.info);
                    }
                }
            });

        }
    });


    $(".users_system_manage").click(function () {
        var roleid = $(this).parent().attr("roleid");
        if (typeof(roleid) == "undefined" || roleid.length <= 0) {
            alert("非法操作");
        } else {
            if ($(this).parents(".users_system_rightList").find(".users_system_managedv").is(":hidden")) {
                $.ajax({
                    url: '${basePath}v2/factory/individual/getsystemuser',
                    type: 'GET',
                    data: {
                        "roleid": roleid
                    },
                    error: function (response) {
                        $errorMsg(response);
                    },
                    success: function (ret) {
                        if (ret.result) {
                            $("#GC_SYSTEMUSER_" + roleid).children().remove();
                            $.each(ret.userinfos, function (index, userinfo) {

                                $childhtml = '<div class="users_system_resetpsddv" id="users_system_resetpsddv' + userinfo.userid + '"><span class="users_system_userName">' +
                                        userinfo.username + '</span><time>' + userinfo.regTime + '</time><a class="users_system_deletebutton" ' +
                                        'href="javascript:;" onclick="showremovesystem(' + userinfo.userid + ')">删除</a><a class="users_system_resetpsd" href="javascript:;" onclick="showresetpassword(' + userinfo.userid + ')">' +
                                        '修改密码</a><a class="users_system_beginpsd" href="javascript:;" onclick="recoverPassowrd(' + userinfo.userid + ')">恢复初始密码</a></div>';
                                $("#GC_SYSTEMUSER_" + roleid).append($childhtml);
                            });

                            $("#GC_SYSTEMUSER_" + roleid).stop().show().siblings("div").hide();
                            $("#users_system_manage_" + roleid).css("background", "#EEE");
                            $("#users_system_manage_" + roleid).siblings("a").css("background", "#FFF");
                        } else {
                            alert(ret.info);
                        }
                    }
                });
            } else {
                $(this).parents(".users_system_rightList").find(".users_system_managedv").stop().hide();
                $(this).css("background", "#FFF");
            }
        }
    });

    $(".pointpage_psdcfm").click(function () {
        var password = $("#GC_SYSTEMUSER_PASSWORD").val();
        var userid = $("#GC_SYSTEMUSER_USERID").val();
        if (typeof(userid) == "undefined" || userid.length <= 0) {
            alert("操作异常");
        } else if (typeof(password) == "undefined" || password.length <= 0) {
            $(".pointpage_psdpoint").text("密码不能为空");
        } else if (!regPass.test(password)) {
            $(".pointpage_psdpoint").text("密码长度至少6位，最大16位");
        } else {
            $.ajax({
                url: '${basePath}v2/factory/individual/changesystemuserpass',
                type: 'POST',
                data: {
                    "userid": userid,
                    "password": createPassword(password, password)
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $(".pointpage_psddv").hide();
                        alert("修改'" + $("#GC_SYSTEMUSER_USERNAME").text() + "'用户密码成功", "cur");
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    });

    <%-- 恢复密码 --%>
    function recoverPassowrd(val) {
        if (typeof(val) == "undefined" || val.length <= 0) {
            alert("操作异常");
            return;
        } else {
            if (confirm("是否恢复'" + $("#users_system_resetpsddv" + val).children("span").text() + "'的初始密码?")) {
                $.ajax({
                    url: '${basePath}v2/factory/individual/recoversystemuserpassword',
                    type: 'POST',
                    data: {
                        "userid": val
                    },
                    error: function (response) {
                        $errorMsg(response);
                    },
                    success: function (ret) {
                        if (ret.result) {
                            alert("恢复'" + $("#users_system_resetpsddv" + val).children("span").text() + "'用户密码成功", "cur");
                        } else {
                            alert(ret.info);
                        }
                    }
                });
            }
        }
    }

    <%-- 删除子账户 --%>
    $(".users_system_deletebtn1").click(function () {
        var val = $("#GC_SYSTEMUSER_REMOVE_USERID").val();
        if (typeof(val) == "undefined" || val.length <= 0) {
            alert("操作异常");
            return;
        } else {
            $(".pointpage_dv").hide();
            $.ajax({
                url: '${basePath}v2/factory/individual/removesystemser',
                type: 'POST',
                data: {
                    "userid": val
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $("#users_system_resetpsddv" + val).remove();
                        alert("系统用户删除成功", "cur");
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    })

    $(".users_system_addchildbtn1").click(function () {
        var modulesArray = new Array();
        var roleid = $(this).attr("GC_ROLEID");
        $("#GC_MODULE_USEDMODULE_" + roleid + " span").each(function (index, obj) {
            modulesArray[index] = $(obj).attr("GC_MODULEID");
        })

        setupmodule(roleid, modulesArray)
    });

    <%-- 设置角色权限 --%>
    function setupmodule(usergroupid, modules) {
        if (typeof(usergroupid) == "undefined" || usergroupid.length <= 0) {
            alert("操作异常");
            return;
        } else if (typeof(modules) == "undefined" || modules.length <= 0) {
            alert("请选择权限后，再保存");
            return;
        } else {
            $.ajax({
                url: '${basePath}v2/individual/setuprolemodules',
                type: 'POST',
                data: {
                    "roleid": usergroupid,
                    "modules": modules
                },
                traditional: true,
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        alert("角色权限分配成功", "cur");
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    }

    <%-- 显示角色权限 --%>
    function showrolemodules(usergroupid) {
        if (typeof(usergroupid) == "undefined" || usergroupid.length <= 0) {
            alert("非法操作");
        } else {
            $.ajax({
                url: '${basePath}v2/factory/individual/getrolemodules',
                type: 'POST',
                data: {
                    "usergroupid": usergroupid
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $("#GC_MODULE_AVAILABLEMODULE_" + usergroupid).children().remove();
                        $.each(ret.availablemodule, function (index, module) {
                            var availablehtml = '<li GC_USERINFO_ROLE_MODULEID="' + module.id + '"><p class="my_childAccountId_' + (module.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module.id + '">' + module.text + '（可选择）</span><a href="javascript:;">' +
                                    '<var class="my_childAccount_addBtn">全部添加</var><var class="my_childAccount_removeBtn">全部删除</var></a></p><ul class="">';
                            $.each(module.children, function (i, module1) {
                                availablehtml = availablehtml + '<li GC_USERINFO_ROLE_MODULEID="' + module1.id + '"><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module1.id + '">' + module1.text + '</span>' +
                                        '<a href="javascript:;"><var class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p>' +
                                        '<ul class="">';

                                $.each(module1.children, function (j, module2) {
                                    availablehtml = availablehtml + '<li GC_USERINFO_ROLE_MODULEID="' + module2.id + '"><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '"><span class="my_childAccount_name" GC_MODULEID="' + module2.id + '">' + module2.text + '</span><a href="javascript:;"><var' +
                                            ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p><ul class="">';

                                    $.each(module2.children, function (l, module3) {
                                        availablehtml = availablehtml + '<li GC_USERINFO_ROLE_MODULEID="' + module3.id + '"><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '_' + module3.id + '"><span GC_MODULEID="' + module3.id + '">' + module3.text + '</span><a href="javascript:;"><var' +
                                                ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p></li>';
                                    });
                                    availablehtml = availablehtml + '</ul></li>';
                                });
                                availablehtml = availablehtml + '</ul></li>';
                            });
                            availablehtml = availablehtml + '</li></ul></li>';

                            $("#GC_MODULE_AVAILABLEMODULE_" + usergroupid).append(availablehtml);
                        });

                        $("#GC_MODULE_USEDMODULE_" + usergroupid).children().remove();
                        $.each(ret.usedmodule, function (index, module) {
                            var usedhtml = '<li><p class="my_childAccountId_' + (module.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module.id + '">' + module.text + '（可选择）</span><a href="javascript:;">' +
                                    '<var class="my_childAccount_addBtn">全部添加</var><var class="my_childAccount_removeBtn">全部删除</var></a></p><ul class="">';

                            $.each(module.children, function (i, module1) {
                                usedhtml = usedhtml + '<li><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module1.id + '">' + module1.text + '</span>' +
                                        '<a href="javascript:;"><var class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p>' +
                                        '<ul class="">';

                                $.each(module1.children, function (j, module2) {
                                    usedhtml = usedhtml + '<li><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '"><span class="my_childAccount_name" GC_MODULEID="' + module2.id + '">' + module2.text + '</span><a href="javascript:;"><var' +
                                            ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p><ul class="">';

                                    $.each(module2.children, function (l, module3) {
                                        usedhtml = usedhtml + '<li><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '_' + module3.id + '"><span GC_MODULEID="' + module3.id + '">' + module3.text + '</span><a href="javascript:;"><var' +
                                                ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p></li>';

                                    });
                                    usedhtml = usedhtml + '</ul></li>';
                                });
                                usedhtml = usedhtml + '</ul></li>';
                            });
                            usedhtml = usedhtml + '</li></ul></li>';
                            $("#GC_MODULE_USEDMODULE_" + usergroupid).append(usedhtml);

                        });
                        //显示授权模块
                        $(".my_childAccount_pointpage").show();
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    }

    function showremovesystem(id) {
        $("#GC_SYSTEMUSER_REMOVE_USERNAME").text($("#users_system_resetpsddv" + id).children("span").text());
        $("#GC_SYSTEMUSER_REMOVE_USERID").val(id);
        $(".pointpage_dv").show();
    }

    function showresetpassword(id) {
        $("#GC_SYSTEMUSER_USERNAME").text($("#users_system_resetpsddv" + id).children("span").text());
        $("#GC_SYSTEMUSER_USERID").val(id);
        $(".pointpage_psddv").show();
    }


</script>